package com.yicong.ants.ui.me;

import android.text.Html;

import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.R;
import com.yicong.ants.bean.global.NoticeRecordItem;
import com.yicong.ants.databinding.NoticeRecordActivityBinding;
import com.yicong.ants.databinding.NoticeRecordItemBinding;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.List;

/**
 * @author cch
 * @version 2020/10/1
 */
public class NoticeRecordActivity extends BaseTitleBarActivity<NoticeRecordActivityBinding> {

    List<NoticeRecordItem> mListBean;

    @Override
    protected int getLayout() {
        return R.layout.notice_record_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("历史公告");
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().noticeRecord()
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                mListBean = respBean.getData();
                updateRecord();
            }, RxHelper.getHideProgressConsumer(this)));
    }

    private void updateRecord() {
        for (NoticeRecordItem item : mListBean) {
            NoticeRecordItemBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.notice_record_item, mDataBind.container, false);
            binding.setBean(item);
            binding.content.setText(Html.fromHtml(item.getContent()));
            mDataBind.container.addView(binding.getRoot());
            binding.getRoot().setOnClickListener(v -> {
                if (StringHelper.isNotEmpty(item.getUrl())) {
                    ZzConfigs.toWebActivity(mContext, item.getTitle(), item.getUrl());
                }
            });
        }
    }
}
